package com.example.demo.util;

import com.sun.org.apache.xml.internal.security.utils.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;

/**
 * Copyright (c)2017
 * 欣网互联网络科技有限公司
 * author: lpc
 * created on: 2018/9/11 0011
 * description:
 */
public class DES {

    private volatile static DES uniqueInstance;

    private static byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8};

    private static String keyVal;

    private DES() {

    }

    private DES(String strKey) {
        keyVal = strKey;
    }

    /**
     * 如果密钥相同则返回单例，如果是新的密钥则创建新的加密类
     *
     * @param strKey
     * @return
     */
    public static DES getInstance(String strKey) {
        if (uniqueInstance == null || !strKey.equals(keyVal)) {
            synchronized (DES.class) {
                if (uniqueInstance == null || !strKey.equals(keyVal)) {
                    uniqueInstance = new DES(strKey);
                    keyVal = strKey;
                }
            }
        }
        return uniqueInstance;
    }

    /**
     * 加密
     *
     * @param encryptString 明文传入
     * @return 密文输出
     */
    public String getEncString(String encryptString) {
        try {
            byte[] encryptedData = getEncCode(encryptString.getBytes("utf-8"));
            return Base64.encode(encryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     *
     * @return 明文输出
     */
    public String getDesString(String decryptString) {
        try {
            byte[] byteMi = Base64.decode(decryptString);
            byte decryptedData[] = getDesCode(byteMi);
            return new String(decryptedData, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public byte[] getEncCode(byte[] byteS) {
        try {
            IvParameterSpec zeroIv = new IvParameterSpec(iv);
            SecretKeySpec key = new SecretKeySpec(keyVal.getBytes(), "DES");
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            byte encryptedData[] = cipher.doFinal(byteS);
            return encryptedData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public byte[] getDesCode(byte[] byteD) {
        try {
            IvParameterSpec zeroIv = new IvParameterSpec(iv);
            SecretKeySpec key = new SecretKeySpec(keyVal.getBytes(), "DES");
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
            byte decryptedData[] = cipher.doFinal(byteD);
            return decryptedData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
//    	
        DES des = DES.getInstance("zsh%e678");
        String str = "18831120114";
        String t = des.getEncString(str);
//    	String t1 = des.getEncString("13643221414");
//    	System.out.println("15200097474=" + t);
        System.out.println("13472107474=" + t);


    }
}
